home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / castools.zip / REMOVEAN.C < prev    next >
Text File  |  1990-02-16  |  4KB  |  128 lines

  1.  
  2.  
  3. /*
  4.    REMOVEAN.C Function PbRemoverEntry:  Removes an entry (individual or group)
  5.       from an open phonebook.
  6.  
  7.    INPUT:  Phonebook structure, and the record id of the entry to remove.
  8.  
  9.    OUTPUT: If successful, removes the entry, and updates the phonebook's
  10.       FreeBytes and entries fields.
  11. */
  12.  
  13. #include <stdio.h>
  14. #include <phonebk.h>
  15.  
  16. int pascal PbRemoveEntry(PB *pb, int RecordID)
  17. {
  18.   int retval = FAIL;        /* Default if anything goes wrong */
  19.   PBE *entry = NULL;
  20.   int i;
  21.   int writ;                 /* for return value from fwrite() */
  22.   int *members;             /* to point to membership list */
  23.   long offset = NULL;       /* for nulling out RecordID */
  24.   int temperrno = 0;        /* for saving aside Pberrno */
  25.  
  26.   Pberrno = 0;                               /* Initially, always reset */
  27.  
  28.   /* First, check params */
  29.   if (pb == NULL) {
  30.     Pberrno = INVALIDPARAMETER;
  31.     return(NULL);
  32.   }
  33.   if ((RecordID < 0) ||
  34.       (RecordID > 999)) {
  35.     Pberrno = INVALIDPARAMETER;
  36.     return(NULL);
  37.   }
  38.  
  39.   /* First, fetch entry record */
  40.   if (!(entry = PbGetEntry(pb, NULL, NULL, RecordID))) {
  41.     Pberrno = OUTOFMEM;
  42.     goto getout;
  43.   }
  44.  
  45.   /* Check if removing entry will put the 'unused bytes' over the limit */
  46.   if ((entry->length +
  47.        pb->header.FreeBytes +
  48.        entry->members * sizeof(int)) > MAXUNUSEDBYTES) {
  49.     Pberrno = TOOMANYFREEBYTES;
  50.     goto getout;
  51.   }
  52.  
  53.   /* Remove the group ID from its members, or the member ID from its groups */
  54.   if (entry->members) {
  55.     if (entry->type == GROUPENTRY) {
  56.       for (i=0; i<entry->members; i++) {
  57.         if (!(PbRemoveFromGroup(pb, RecordID, entry->MemberList[i]))) {
  58.           goto getout;            /* Pberrno set by -Remove- function */
  59.         }
  60.       }
  61.     }
  62.     else {
  63.       for (i=0; i<entry->members; i++) {
  64.         if (!(PbRemoveFromGroup(pb, entry->MemberList[i], RecordID))) {
  65.           goto getout;            /* Pberrno set by -Remove- function */
  66.         }
  67.       }
  68.     }
  69.  
  70.     /* RemoveFromGroup doesn't update the structure we're using, so we have to*/
  71.     entry->length -= entry->members * sizeof(int);
  72.   }
  73.  
  74.   /* Update phonebook fields, set offset at RecordID to null */
  75.   pb->header.FreeBytes += entry->length;
  76.   if (fseek(pb->fp, 6L,  SEEK_SET)) {
  77.     Pberrno = FSEEKERROR;
  78.     goto getout;
  79.   }
  80.   writ = fwrite(&pb->header.FreeBytes, sizeof(int), 1, pb->fp);
  81.   if (writ != 1) {
  82.     Pberrno = CANTWRITE;
  83.     goto getout;
  84.   }
  85.   pb->header.entries--;
  86.   if (fseek(pb->fp, 4L,  SEEK_SET)) {
  87.     Pberrno = FSEEKERROR;
  88.     goto getout;
  89.   }
  90.   writ = fwrite(&pb->header.entries, sizeof(int), 1, pb->fp);
  91.   if (writ != 1) {
  92.     Pberrno = CANTWRITE;
  93.     goto getout;
  94.   }
  95.   if (fseek(pb->fp, 160L + RecordID * sizeof(LONGWORD), SEEK_SET)) {
  96.     Pberrno = FSEEKERROR;
  97.     goto getout;
  98.   }
  99.   writ = fwrite(&offset, sizeof(LONGWORD), 1, pb->fp);
  100.   if (writ != 1) {
  101.     Pberrno = CANTWRITE;
  102.     goto getout;
  103.   }
  104.  
  105.   /* And finally, the OBuffer */
  106.   if (pb->OBuffer) {
  107.     if ((RecordID >= pb->FirstOBufferRID) &&
  108.         ((RecordID - pb->FirstOBufferRID) * sizeof(LONGWORD) < pb->OBufferSize)) {
  109.       pb->OBuffer[RecordID - pb->FirstOBufferRID] = offset;
  110.     }
  111.   }
  112.  
  113.   /* If we got here, all went well */
  114.   retval = SUCCESS;
  115.  
  116. getout:
  117.   if (Pberrno) {
  118.     temperrno = Pberrno;
  119.   }
  120.   if (entry) {
  121.     PbFreePBE(pb, entry);
  122.   }
  123.   if (temperrno) {
  124.     Pberrno = temperrno;
  125.   }
  126.   return(retval);
  127. }
  128.